package com.example.score.config;

import com.example.score.account.Manager;
import com.example.score.score.ConfirmSocket;
import com.example.score.score.JudgeSocket;
import com.example.score.score.SocketScore;
import com.example.score.score.ViewSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.annotation.Resource;
import java.util.Map;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Resource
    SocketScore socketScore;

    @Autowired
    Manager manager;

    @Resource
    JudgeSocket judgeSocket;

    @Resource
    ViewSocket viewSocket;

    @Resource
    ConfirmSocket confirmSocket;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(socketScore, "/score/*")
                .setAllowedOrigins("*")
                .addInterceptors(pathParamInterceptor());
        registry.addHandler(judgeSocket, "/judge/*")
                .setAllowedOrigins("*")
                .addInterceptors(judgePathParameter());
        registry.addHandler(viewSocket, "/view/*")
                .setAllowedOrigins("*")
                .addInterceptors(viewPathParameter());
        registry.addHandler(confirmSocket, "/confirm/*")
                .setAllowedOrigins("*");
    }

    @Bean
    public HandshakeInterceptor viewPathParameter() {
        return new HandshakeInterceptor() {
            /**
             * 获取裁判ID，并进行验证
             * @param request
             * @param response
             * @param wsHandler
             * @param attributes
             * @return
             * @throws Exception
             */
            @Override
            public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
                String path = request.getURI().getPath();
                String refereeId = path.substring(path.lastIndexOf('/') + 1);
                if (manager.verifyRefereeId(refereeId)) {
                    attributes.put("refereeId", refereeId);
                    return true;
                }
                return false;
            }

            @Override
            public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
            }
        };
    }

    @Bean
    public HandshakeInterceptor judgePathParameter() {
        return new HandshakeInterceptor() {
            /**
             * 获取裁判ID，并进行验证
             * @param request
             * @param response
             * @param wsHandler
             * @param attributes
             * @return
             * @throws Exception
             */
            @Override
            public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
                String path = request.getURI().getPath();
                String judgeId = path.substring(path.lastIndexOf('/') + 1);
                if (manager.verifyJudgeId(judgeId)) {
                    attributes.put("judgeId", judgeId);
                    return true;
                } else {
                    return false;
                }
            }

            @Override
            public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
            }
        };
    }



    @Bean
    public HandshakeInterceptor pathParamInterceptor() {
        return new HandshakeInterceptor() {
            /**
             * 获取referee裁判长ID，并进行验证
             * @param request
             * @param response
             * @param wsHandler
             * @param attributes
             * @return
             * @throws Exception
             */
            @Override
            public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
                String path = request.getURI().getPath();
                String refereeId = path.substring(path.lastIndexOf('/') + 1);
                if (manager.verifyRefereeId(refereeId)) {
                    attributes.put("refereeId", refereeId);
                    return true;
                } else {
                    return false;
                }
            }

            @Override
            public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
            }
        };
    }
}
